{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Determine Whether Matrix Can Be Obtained By Rotation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findRotation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #判断矩阵经轮转后是否一致"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个大小为 <code>n x n</code> 的二进制矩阵 <code>mat</code> 和 <code>target</code> 。现<strong> 以 90 度顺时针轮转 </strong>矩阵 <code>mat</code> 中的元素 <strong>若干次</strong> ，如果能够使 <code>mat</code> 与 <code>target</code> 一致，返回 <code>true</code> ；否则，返回<em> </em><code>false</code><em> 。</em></p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/05/20/grid3.png\" style=\"width: 301px; height: 121px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[0,1],[1,0]], target = [[1,0],[0,1]]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>顺时针轮转 90 度一次可以使 mat 和 target 一致。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/05/20/grid4.png\" style=\"width: 301px; height: 121px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[0,1],[1,1]], target = [[1,0],[0,1]]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>无法通过轮转矩阵中的元素使 equal 与 target 一致。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/05/26/grid4.png\" style=\"width: 661px; height: 184px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>顺时针轮转 90 度两次可以使 mat 和 target 一致。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == mat.length == target.length</code></li>\n",
    "\t<li><code>n == mat[i].length == target[i].length</code></li>\n",
    "\t<li><code>1 <= n <= 10</code></li>\n",
    "\t<li><code>mat[i][j]</code> 和 <code>target[i][j]</code> 不是 <code>0</code> 就是 <code>1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [determine-whether-matrix-can-be-obtained-by-rotation](https://leetcode.cn/problems/determine-whether-matrix-can-be-obtained-by-rotation/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [determine-whether-matrix-can-be-obtained-by-rotation](https://leetcode.cn/problems/determine-whether-matrix-can-be-obtained-by-rotation/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[0,1],[1,0]]\\n[[1,0],[0,1]]', '[[0,1],[1,1]]\\n[[1,0],[0,1]]', '[[0,0,0],[0,1,0],[1,1,1]]\\n[[1,1,1],[0,1,0],[0,0,0]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = [list(l) for l in zip(*mat[::-1])]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        for k in range(4):\n",
    "            for i in range(n//2):\n",
    "                for j in range((n+1)//2):\n",
    "                    mat[i][j], mat[n-1-j][i],mat[n-1-i][n-1-j],mat[j][n-1-i] \\\n",
    "                    = mat[n-1-j][i], mat[n-1-i][n-1-j],mat[j][n-1-i],mat[i][j]\n",
    "            if mat==target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat==target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat=[list(i)[::-1]for i in zip(*mat)]\n",
    "            if mat==target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# ### 参考48题，官2解释\n",
    "# class Solution:\n",
    "#     def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "#         n=len(mat)\n",
    "#         for k in range(4):\n",
    "#             ### 旋转操作(4次，返回原始mat)\n",
    "#             for i in range(n//2):\n",
    "#                 for j in range((n+1)//2): ### n为奇数时的枚举个数\n",
    "#                     # mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j],mat[j][n-1-i]=\n",
    "#                     # mat[j][n-1-i],mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j]\n",
    "#                    ### 新位置=旧位置\n",
    "#                     mat[j][n-1-i],mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j]=        mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j],mat[j][n-1-i]\n",
    "#             if mat==target:\n",
    "#                 return True \n",
    "#         return False\n",
    "\n",
    "### 官码：\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        # 最多旋转 4 次\n",
    "        for k in range(4):\n",
    "            # 旋转操作\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        mat = [tuple(l) for l in mat]\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        def rotate(matrix):\n",
    "            m,n = len(matrix),len(matrix[0])\n",
    "            new = [[0] * m for _ in range(n)]\n",
    "            for i in range(m):\n",
    "                for j in range(n):\n",
    "                    new[j][m-1-i] = matrix[i][j]\n",
    "            return new\n",
    "        \n",
    "        if mat == target:\n",
    "            return True\n",
    "        \n",
    "        for i in range(3):\n",
    "            mat = rotate(mat)\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        mat = [tuple(l) for l in mat]\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        target = [tuple(i) for i in target]\n",
    "        for _ in range(3):\n",
    "            mat = list(zip(*mat))[::-1]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        # n = len(mat)\n",
    "        # if all(mat[i][j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        # or all(mat[j][n - 1 - i] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        # or all(mat[n - 1 - i][n - 1 - j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        # or all(mat[n - 1 - j][i] == target[i][j] for i in range(n) for j in range(n)) :\n",
    "        #     return True\n",
    "        # return False\n",
    "\n",
    "\n",
    "        if mat == target :\n",
    "            return True\n",
    "        for _ in range(3) :\n",
    "            mat = [list(reversed(row)) for row in zip(*mat)]\n",
    "            if mat == target :\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "                \n",
    "        def rotate(mat):            \n",
    "            tx=[[0 for _ in range(len(mat[0]))] for _ in range(len(mat))]\n",
    "            for i in range(len(mat)):\n",
    "                for j in range(len(mat[0])):\n",
    "                    tx[j][len(mat)-i-1]=mat[i][j]                    \n",
    "            print(tx)\n",
    "            return tx\n",
    "        \n",
    "        t=mat\n",
    "        for i in range(4):\n",
    "            t=rotate(t)\n",
    "            if t==target:\n",
    "                return True            \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        \n",
    "        n = len(mat)\n",
    "        if all(mat[i] == target[i] for i in range(n)):\n",
    "            return True\n",
    "        ret = [[0] * n for _ in range(n)]\n",
    "        for time in range(3):\n",
    "            for i in range(n):\n",
    "                for j in range(n):\n",
    "                    i2 = j\n",
    "                    j2 = n - i - 1\n",
    "                    ret[i2][j2] = mat[i][j]\n",
    "            if all(ret[i] == target[i] for i in range(n)):\n",
    "                return True\n",
    "            for i in range(n):\n",
    "                for j in range(n):\n",
    "                    mat[i][j] = ret[i][j]\n",
    "        return False\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        \n",
    "        # m = len(mat)\n",
    "        n = len(mat)\n",
    "        #判断旋转90\n",
    "        cnt1 = 0\n",
    "        cnt2 = 0\n",
    "        cnt3 = 0\n",
    "        cnt4 = 0\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] == target[j][n-1-i]:\n",
    "                    # print(i,j,j,n-1-i)\n",
    "                    cnt1 += 1\n",
    "                if mat[i][j] == target[n-1-j][i]:\n",
    "                    cnt2 += 1\n",
    "                    # print(i,j)\n",
    "                if mat[i][j] == target[n-1-i][n-1-j]:\n",
    "                    cnt3 += 1\n",
    "                if mat[i][j] == target[i][j]:\n",
    "                    cnt4 += 1\n",
    "        if cnt1 == n*n or cnt2 == n*n or cnt3 ==n*n or cnt4 == n*n:\n",
    "            return True\n",
    "        return False\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        # n = len(mat)\n",
    "        # if all(mat[i][j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        # or all(mat[j][n - 1 - i] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        # or all(mat[n - 1 - i][n - 1 - j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        # or all(mat[n - 1 - j][i] == target[i][j] for i in range(n) for j in range(n)) :\n",
    "        #     return True\n",
    "        # return False\n",
    "\n",
    "\n",
    "        if mat == target :\n",
    "            return True\n",
    "        for _ in range(3) :\n",
    "            mat = [list(reversed(row)) for row in zip(*mat)]\n",
    "            if mat == target :\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        mat = [tuple(l) for l in mat]\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = [list(i)[::-1] for i in zip(*mat)]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        for i in range(4):\n",
    "            mat= list(map(list,zip(*mat[::-1])))\n",
    "            if mat==target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "\n",
    "        for x in range(4):\n",
    "            for i in range(n//2):\n",
    "                for j in range((n+1)//2):\n",
    "                    tmp = mat[i][j]\n",
    "                    mat[i][j] = mat[n-j-1][i]\n",
    "                    mat[n-j-1][i] = mat[n-i-1][n-j-1]\n",
    "                    mat[n-i-1][n-j-1] = mat[j][n-i-1]\n",
    "                    mat[j][n-i-1] = tmp\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        for i in range(4):\n",
    "            mat = [list(l[::-1]) for l in zip(*mat)]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(target)\n",
    "        cnt_0, cnt_90, cnt_180, cnt_270 = True, True, True, True\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                x = target[i][j]\n",
    "                if x != mat[i][j]: cnt_0 = False\n",
    "                if x != mat[j][n - 1 - i]: cnt_270 = False\n",
    "                if x != mat[n - 1- i][n - 1 - j]: cnt_180 = False\n",
    "                if x != mat[n - 1 - j][i]: cnt_90 = False\n",
    "        return cnt_0 or cnt_90 or cnt_180 or cnt_270"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        for _ in range(4):\n",
    "            mat[:] = map(list, zip(*mat[::-1]))\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        if all(mat[i][j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[j][n - 1 - i] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[n - 1 - i][n - 1 - j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[n - 1 - j][i] == target[i][j] for i in range(n) for j in range(n)) :\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def matricRotate(self, matrix):\n",
    "        result = []\n",
    "        for j in range(len(matrix[0])):\n",
    "            temp = []\n",
    "            for i in range(len(matrix)-1, -1, -1):\n",
    "                temp.append(matrix[i][j])\n",
    "            result.append(temp)\n",
    "        return result\n",
    "\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        for i in range(4, ):\n",
    "            if mat == target:\n",
    "                return True\n",
    "            mat = self.matricRotate(mat)\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        if mat == target :\n",
    "            return True\n",
    "        \n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if mat[n-1-j][i] != target[i][j]:\n",
    "                    break\n",
    "            else:\n",
    "                continue\n",
    "            break\n",
    "        else:\n",
    "            return True\n",
    "        \n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if mat[n-1-i][n-1-j] != target[i][j]:\n",
    "                    break\n",
    "            else:\n",
    "                continue\n",
    "            break\n",
    "        else:\n",
    "            return True\n",
    "\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if mat[j][n-1-i] != target[i][j]:\n",
    "                    break\n",
    "            else:\n",
    "                continue\n",
    "            break\n",
    "        else:\n",
    "            return True\n",
    "\n",
    "        return False\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "                \n",
    "        def rotate(mat):            \n",
    "            tx=[[0 for _ in range(len(mat[0]))] for _ in range(len(mat))]\n",
    "            for i in range(len(mat)):\n",
    "                for j in range(len(mat[0])):\n",
    "                    tx[j][len(mat)-i-1]=mat[i][j]                    \n",
    "            #print(tx)\n",
    "            return tx\n",
    "        \n",
    "        t=mat\n",
    "        for i in range(4):\n",
    "            t=rotate(t)\n",
    "            if t==target:\n",
    "                return True            \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        for q in range(4):\n",
    "            if mat==target:\n",
    "                return True\n",
    "            mat = [list(x)[::-1] for x in zip(*mat)]\n",
    "        return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        m=mat\n",
    "        for a in range(4):\n",
    "            m = list(zip(*m))\n",
    "            x = []\n",
    "            for i in m:\n",
    "                x.append(list(i)[::-1])\n",
    "                m=x\n",
    "            if m==target:\n",
    "                return True\n",
    "                break\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \"\"\"\n",
    "    旋转坐标的关系：https://leetcode.cn/problems/determine-whether-matrix-can-be-obtained-by-rotation/description/comments/1962616\n",
    "    转度数          原坐标         转换坐标\n",
    "        90°            [x, y]   ->    [y, n - x - 1]\n",
    "        180°           [x, y]   ->    [n - x - 1, n - y - 1]\n",
    "        270°           [x, y]   ->    [n - y - 1, x]\n",
    "    \"\"\"\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        def equal(mat, target, rows, cols):\n",
    "            for i in range(rows):\n",
    "                for j in range(cols):\n",
    "                    if mat[i][j] != target[i][j]:\n",
    "                        return False\n",
    "            return True\n",
    "        rows, cols = mat.__len__(), mat[0].__len__()\n",
    "        k = rows // cols\n",
    "        import copy\n",
    "        for roundtime in range(1, 5):\n",
    "            if equal(mat, target, rows, cols):\n",
    "                return True\n",
    "            mat_ori = copy.deepcopy(mat)\n",
    "            for i in range(rows):\n",
    "                for j in range(cols):\n",
    "                    mat[j][rows-i-1] = mat_ori[i][j]\n",
    "            mat_ori = copy.deepcopy(mat)\n",
    "            \n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        for i in range(4):\n",
    "            mat = self.rotate_matrix(mat)\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n",
    "    def rotate_matrix(self,matrix):  \n",
    "        m = len(matrix)  \n",
    "        n = len(matrix[0])  \n",
    "        rotated_matrix = [[0 for _ in range(m)] for _ in range(n)]  \n",
    "        for i in range(m):  \n",
    "            for j in range(n):  \n",
    "                rotated_matrix[j][m-i-1] = matrix[i][j]  \n",
    "        return rotated_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        temp = mat\n",
    "        for _ in range(4):\n",
    "            if temp == target:\n",
    "                return True\n",
    "            temp = list(map(list, zip(*temp[::-1])))\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = [list(i)[::-1] for i in zip(*mat)]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat==target:\n",
    "            return True\n",
    "        for step in range(3):\n",
    "            #翻转就是将原矩阵每一列元素逆序后放到新矩阵的某一行\n",
    "            mat = [list(row)[::-1] for row in zip(*mat)]\n",
    "            if mat==target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n=len(mat)\n",
    "        for k in range(4):\n",
    "            for i in range(n//2):\n",
    "                for j in range((n+1)//2):\n",
    "                    mat[i][j], mat[n-1-j][i],mat[n-1-i][n-1-j],mat[j][n-1-i]=mat[n-1-j][i],mat[n-1-i][n-1-j],mat[j][n-1-i],mat[i][j]\n",
    "            if mat==target:\n",
    "                return True\n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat==target:\n",
    "            return True\n",
    "        for step in range(3):\n",
    "            #翻转就是将原矩阵每一列元素逆序后放到新矩阵的某一行\n",
    "            mat = [list(row)[::-1] for row in zip(*mat)]\n",
    "            if mat==target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        # 最多旋转 4 次\n",
    "        for k in range(4):\n",
    "            # 旋转操作\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        return target in [(mat := list(map(list, zip(*mat[::-1])))) for i in range(4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        a=[]\n",
    "        b=[]\n",
    "        n=len(mat)\n",
    "        for i in range(n):\n",
    "            a.append(mat[i])\n",
    "        for j in range(n-1,-1,-1):\n",
    "            a.append([mat[i][j] for i in range(n)])\n",
    "        for i in range(n-1,-1,-1):\n",
    "            a.append([mat[i][j] for j in range(n-1,-1,-1)])\n",
    "        for j in range(n):\n",
    "            a.append([mat[i][j] for i in range(n-1,-1,-1)])\n",
    "        for i in range(n):\n",
    "            b.append(target[i])\n",
    "        for j in range(n-1,-1,-1):\n",
    "            b.append([target[i][j] for i in range(n)])\n",
    "        for i in range(n-1,-1,-1):\n",
    "            b.append([target[i][j] for j in range(n-1,-1,-1)])\n",
    "        for j in range(n):\n",
    "            b.append([target[i][j] for i in range(n-1,-1,-1)])\n",
    "        for _ in range(4):\n",
    "            for _ in range(n):\n",
    "                aa=a.pop()\n",
    "                a.insert(0,aa)\n",
    "            if a==b:\n",
    "                return True\n",
    "        return False\n",
    "         \n",
    "        \n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "### 参考48题，官2解释\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n=len(mat)\n",
    "        for k in range(4):\n",
    "            ### 旋转操作(4次，返回原始mat)\n",
    "            for i in range(n//2):\n",
    "                for j in range((n+1)//2): ### n为奇数时的枚举个数\n",
    "                    # mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j],mat[j][n-1-i]=\n",
    "                    # mat[j][n-1-i],mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j]\n",
    "                   ### 新位置=旧位置\n",
    "                    mat[j][n-1-i],mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j]=        mat[i][j],mat[n-1-j][i],mat[n-1-i][n-1-j],mat[j][n-1-i]\n",
    "            if mat==target:\n",
    "                return True \n",
    "        return False\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        i = 0\n",
    "        for k in range(4):\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n - 1 - j][i], mat[n - 1 - i][n - 1 - j], mat[j][n - 1 - i] = mat[n - 1 - j][i], mat[n - 1 - i][n - 1 - j], mat[j][n - 1- i], mat[i][j]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        m = len(mat)\n",
    "        res = [[0 for i in range(m)] for j in range(m)]\n",
    "        # print(res)\n",
    "        cnt = 0\n",
    "        while cnt <= 4:\n",
    "            res = [[0 for i in range(m)] for j in range(m)]\n",
    "            for i in range(m):\n",
    "                for j in range(m):\n",
    "                    res[j][m - 1 - i] = mat[i][j]\n",
    "            flag = True\n",
    "            for i in range(m):\n",
    "                for j in range(m):\n",
    "                    if res[i][j] != target[i][j]:\n",
    "                        flag = False\n",
    "                        break\n",
    "            if flag:\n",
    "                return True\n",
    "            # print(res)\n",
    "            mat = res\n",
    "            cnt += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rotate_matrix(self, matrix):\n",
    "        n = len(matrix)\n",
    "\n",
    "        # 创建一个新的二维矩阵来存储旋转后的结果\n",
    "        rotated_matrix = [[0] * n for _ in range(n)]\n",
    "\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                # 旋转的操作是将 matrix[i][j] 移动到 rotated_matrix[j][n-1-i] 的位置上\n",
    "                rotated_matrix[j][n-1-i] = matrix[i][j]\n",
    "\n",
    "        return rotated_matrix\n",
    "\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "\n",
    "        new_mat = mat\n",
    "\n",
    "        counter = 0\n",
    "\n",
    "        while counter<=4:\n",
    "            counter+=1\n",
    "\n",
    "            new_mat = self.rotate_matrix(new_mat)\n",
    "\n",
    "            if new_mat==target:\n",
    "                return True\n",
    "            \n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        if all(mat[i][j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[j][n - 1 - i] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[n - 1 - i][n - 1 - j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[n - 1 - j][i] == target[i][j] for i in range(n) for j in range(n)) :\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        # 最多旋转 4 次\n",
    "        for k in range(4):\n",
    "            # 旋转操作\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        a=[]\n",
    "        b=[]\n",
    "        n=len(mat)\n",
    "        for i in range(n):\n",
    "            a.append(mat[i])\n",
    "        for j in range(n-1,-1,-1):\n",
    "            a.append([mat[i][j] for i in range(n)])\n",
    "        for i in range(n-1,-1,-1):\n",
    "            a.append([mat[i][j] for j in range(n-1,-1,-1)])\n",
    "        for j in range(n):\n",
    "            a.append([mat[i][j] for i in range(n-1,-1,-1)])\n",
    "        for i in range(n):\n",
    "            b.append(target[i])\n",
    "        for j in range(n-1,-1,-1):\n",
    "            b.append([target[i][j] for i in range(n)])\n",
    "        for i in range(n-1,-1,-1):\n",
    "            b.append([target[i][j] for j in range(n-1,-1,-1)])\n",
    "        for j in range(n):\n",
    "            b.append([target[i][j] for i in range(n-1,-1,-1)])\n",
    "        print(a)\n",
    "        print(b)\n",
    "        for _ in range(4):\n",
    "            for _ in range(n):\n",
    "                aa=a.pop()\n",
    "                a.insert(0,aa)\n",
    "            if a==b:\n",
    "                return True\n",
    "        return False\n",
    "         \n",
    "        \n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        # 最多旋转 4 次\n",
    "        for k in range(4):\n",
    "            # 旋转操作\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        mat = [tuple(l) for l in mat]\n",
    "        for i in range(3):\n",
    "            mat = [*map(list, zip(*mat[::-1]))]\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        ok = True\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] != target[i][j]:\n",
    "                    ok = False\n",
    "                    break\n",
    "        if ok: return ok\n",
    "        # 左下\n",
    "        idx = 0\n",
    "        ok = True\n",
    "        for j in range(n):\n",
    "            for i in range(n - 1, -1, -1):\n",
    "                if mat[i][j] != target[idx // n][idx % n]:\n",
    "                    ok = False\n",
    "                    break\n",
    "                idx += 1\n",
    "        if ok: return ok\n",
    "        # 右下\n",
    "        idx = 0\n",
    "        ok = True\n",
    "        for i in range(n - 1, -1, -1):\n",
    "            for j in range(n - 1, -1, -1):\n",
    "                if mat[i][j] != target[idx // n][idx % n]:\n",
    "                    ok = False\n",
    "                    break\n",
    "                idx += 1\n",
    "        if ok: return ok\n",
    "        # 右上\n",
    "        idx = 0\n",
    "        ok = True\n",
    "        for j in range(n - 1, -1, -1):\n",
    "            for i in range(n):\n",
    "                if mat[i][j] != target[idx // n][idx % n]:\n",
    "                    ok = False\n",
    "                    break\n",
    "                idx += 1\n",
    "        if ok: return ok\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        # 最多旋转 4 次\n",
    "        for k in range(4):\n",
    "            # 旋转操作\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        # 最多旋转 4 次\n",
    "        for k in range(4):\n",
    "            # 旋转操作\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        if all(mat[i][j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[j][n - 1 - i] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[n - 1 - i][n - 1 - j] == target[i][j] for i in range(n) for j in range(n)) \\\n",
    "        or all(mat[n - 1 - j][i] == target[i][j] for i in range(n) for j in range(n)) :\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \"\"\"\n",
    "    转度数          原坐标         转换坐标\n",
    "        90°            [x, y]   ->    [y, n - x - 1]\n",
    "        180°           [x, y]   ->    [n - x - 1, n - y - 1]\n",
    "        270°           [x, y]   ->    [n - y - 1, x]\n",
    "    \"\"\"\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        def equal(mat, target, rows, cols):\n",
    "            for i in range(rows):\n",
    "                for j in range(cols):\n",
    "                    if mat[i][j] != target[i][j]:\n",
    "                        return False\n",
    "            return True\n",
    "        rows, cols = mat.__len__(), mat[0].__len__()\n",
    "        k = rows // cols\n",
    "        import copy\n",
    "        for roundtime in range(1, 5):\n",
    "            if equal(mat, target, rows, cols):\n",
    "                return True\n",
    "            mat_ori = copy.deepcopy(mat)\n",
    "            for i in range(rows):\n",
    "                for j in range(cols):\n",
    "                    mat[j][rows-i-1] = mat_ori[i][j]\n",
    "            mat_ori = copy.deepcopy(mat)\n",
    "            \n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        for _ in range(4):\n",
    "            mat[:] = map(list, zip(*mat[::-1]))\n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        # 最多旋转 4 次\n",
    "        for k in range(4):\n",
    "            # 旋转操作\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range (3):\n",
    "            mat=[list(i)[ : :-1] for i in zip(*mat)]\n",
    "            if mat==target:\n",
    "                return True\n",
    "        return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        def rot(matrix):\n",
    "            m,n = len(mat), len(mat[0])\n",
    "            new = [[0]*m for _ in range(n)]\n",
    "            for i in range(m):\n",
    "                for j in range(n):\n",
    "                    new[j][m-1-i] = mat[i][j]\n",
    "            return new\n",
    "        if mat == target:\n",
    "            return True\n",
    "        for i in range(3):\n",
    "            mat = rot(mat)\n",
    "            if target == mat:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat==target:\n",
    "            return True\n",
    "        else:\n",
    "            for _ in range(3):\n",
    "                n = len(mat)\n",
    "                new=[[0]*n for _ in range(n)]\n",
    "                for i in range(n):\n",
    "                    for j in range(n):\n",
    "                        new[j][n-1-i] = mat[i][j]\n",
    "                mat = new\n",
    "                if mat == target:\n",
    "                    return True\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        if mat == target:\n",
    "            return True\n",
    "\n",
    "        # num1_1 = 0\n",
    "        # num2_1 = 0\n",
    "        # for item1 in mat:\n",
    "        #     for item2 in item1:\n",
    "        #         if item2 == 1:\n",
    "        #             num1_1 += 1\n",
    "\n",
    "        # for item1 in target:\n",
    "        #     for item2 in item1:\n",
    "        #         if item2 == 1:\n",
    "        #             num2_1 += 1\n",
    "\n",
    "        # if num1_1 != num2_1:\n",
    "        #     return False\n",
    "        \n",
    "        n = len(mat)\n",
    "        # for i in range(n):\n",
    "        #     c=0\n",
    "        #     b=n\n",
    "        #     d=0\n",
    "        #     for j in range(n):\n",
    "        #         if target[n-j-1][c] == mat[i][j]:\n",
    "        #             c=c+1\n",
    "        #         else:\n",
    "        #             return False        \n",
    "        # return True\n",
    "\n",
    "        for j in range(3):\n",
    "            newList = []\n",
    "            for i in range(n):\n",
    "                tmpList = []\n",
    "                for item in mat:\n",
    "                    tmpList.append(item[i])\n",
    "                newList.append(tmpList)\n",
    "            \n",
    "            for i in range(n):         \n",
    "               newList[i] = newList[i][::-1]\n",
    "            \n",
    "            if newList == target:\n",
    "                return True\n",
    "            else:\n",
    "                mat = newList\n",
    "        \n",
    "        return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        def turn(array):\n",
    "            n = len(array)\n",
    "            temp = [[0 for _ in range(n)] for _ in range(n)]\n",
    "            for r in range(n):\n",
    "                for c in range(n):\n",
    "                    temp[c][n - r - 1] = array[r][c]\n",
    "            return temp\n",
    "\n",
    "        m = mat\n",
    "        for _ in range(4):\n",
    "            if m == target:\n",
    "                return True\n",
    "            m = turn(m)\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n",
    "        n = len(mat)\n",
    "        \n",
    "        for k in range(4):  # 最多旋转 4 次\n",
    "            for i in range(n // 2):\n",
    "                for j in range((n + 1) // 2):\n",
    "                    mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] \\\n",
    "                        = mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]\n",
    "            \n",
    "            if mat == target:\n",
    "                return True\n",
    "        \n",
    "        return False"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
